﻿using System;
using System.Windows.Forms;
using BeiraBaga.Output;
using DevExpress.XtraGrid.Views.Grid;
using E4RPPrintManager;
using System.Collections.Generic;

namespace BeiraBaga
{
    public partial class BarCodes : UserControl
    {
        #region Members

        private Controller _controller;

        #endregion

        #region Properties

        public Controller Controller { set { _controller = value; } }

        public GridView GridView { get { return gridView; } }

        #endregion

        #region Public Methods

        public BarCodes()
        {
            InitializeComponent();
        }

        public void Print(int copies)
        {
            var barCodesDataSet = GetBarCodesDataSet(copies);
            if (barCodesDataSet.CodigosBarras.Count == 0)
            {
                MessageBox.Show("Por favor, seleccione os códigos de barras que deseja imprimir.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            var printPreviewXtraForm = new PrintPreviewXtraForm();
            printPreviewXtraForm.Setup(barCodesDataSet, "BarCodesCrystalReport.rpt");
            printPreviewXtraForm.ShowDialog();
        }

        public void Reload()
        {
            tDU_EquivalenciasCBarrasBindingSource.DataSource = null;
            tDU_EquivalenciasCBarrasBindingSource.DataSource = _controller.BarCodes;
        }

        #endregion

        #region Protected Overrides

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (_controller == null)
                return;
            tDU_EquivalenciasCBarrasBindingSource.DataSource = _controller.BarCodes;
            artigoBindingSource.DataSource = _controller.Products;
            employeesViewBindingSource.DataSource = _controller.Employees;
        }

        #endregion

        #region Event Handlers

        private void gridView_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            var barCode = (TDU_EquivalenciasCBarras)e.Row;
            _controller.Save(barCode);
        }        

        #endregion

        #region Private Methods

        private BarCodesDataSet GetBarCodesDataSet(int copies)
        {
            var selectedBarCodes = new List<TDU_EquivalenciasCBarras>();
            foreach (int rowHandle in gridView.GetSelectedRows())
            {
                var barCode = (TDU_EquivalenciasCBarras) gridView.GetRow(rowHandle);
                if (barCode == null)
                    continue;
                for (var copy = 0; copy < copies; copy++)
                    selectedBarCodes.Add(barCode);
            }
            return _controller.GetBarCodesDataset(selectedBarCodes);
        }

        private void tDU_EquivalenciasCBarrasBindingSource_CurrentItemChanged(object sender, EventArgs e)
        {
            _controller.Save();
        }

        #endregion        
    }
}
